package com.zy.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.test.entity.Room;
import com.zy.test.service.RoomService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 教室表：
 * 教室ID，教室名，备注。教室ID为主键。(Room)表控制层
 *
 * @author jhr
 * @since 2023-04-03 16:32:30
 */
@RestController
@RequestMapping("room")
public class RoomController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private RoomService roomService;

    /**
     * 分页查询所有数据
     *
     * @param room 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectRoomPaging")
    public R selectAllPaging(Room room) {
        Page<Room> page = new Page<>(room.getPageNum(),room.getPageSize());
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        if (room.getRoCode()!=null){
            queryWrapper.eq(Room::getRoCode,room.getRoCode());
        }
        if (room.getRoName()!=null){
            queryWrapper.like(Room::getRoName,room.getRoName());
        }
        return success(this.roomService.page(page,queryWrapper));
    }

    @GetMapping("/selectAll")
    public R selectAll() {
        return success(this.roomService.list());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.roomService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param room 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public R insert(@RequestBody Room room) {
        return success(this.roomService.save(room));
    }

    /**
     * 修改数据
     *
     * @param room 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public R update(@RequestBody Room room) {
        return success(this.roomService.updateById(room));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteByIds")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.roomService.removeByIds(idList));
    }
}

